@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <style>
    </style>
    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <form action="/{{ request()->path() }}/../" method="post" class="new-precut-form t-right">
            @csrf
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <input type="text" name="_prec_" value="{{ $data->precut_id }}" hidden>
            <table class="table s3">
                <tbody>
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                </tr>
                {{--<tr>
                    <td class="tth">{{ ts('SCSL') }}</td>
                    <td class="ttd">
                        <input type="number" name="number" class="form-control">
                    </td>
                    <td colspan="4" class="text-danger"></td>
                </tr>--}}
                <tr>
                    <td colspan="6" class="ttt">
                        <div class="new-precut-coverage">
                            <table class="table table-s3 coverage-table text-nowrap align-top ">
                                <thead>
                                <tr>
                                    <th style="width: 20%">{{ ts('ZTBW') }}</th>
                                    <th style="width: 10%">{{ ts('ylcd2') }}</th>
                                    <th style="width: 10%">{{ ts('SCSL') }}</th>

                                    <th style="width: 10%">{{ ts('PRODUCTTYPE') }}</th>
                                    <th style="width: 10%">{{ ts('MXH') }}</th>
                                    <th style="width: 10%"></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>

                            <div class="my-5 pl-5">
                                <button type="button" class="btn btn-info add-more" onclick="add()">{{ ts('addmore') }}</button>
                            </div>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="mt-4">
                <button type="button" class="btn btn-primary" onclick="savePrecut(this)">{{ ts('store') }}</button>
            </div>
        </form>
    </div>


@endsection

@section('script')
    <script>
        var filmTypeRlen = {};
        function updateRestlen() {
            var items = $('.new-precut-coverage').find('.item');
            for (var i = 0; i <= items.length - 1; i++) {
                var fkey = items.eq(i).find('[name="film_type_id[]"]').val();
                console.log('adsasd', items.length, fkey)
                if (fkey) {
                    var restlen = filmTypeRlen[fkey];
                    if (restlen !== undefined) {
                        items.eq(i).find('.residue-length b').text(restlen)
                        items.eq(i).find('.residue-length').removeClass('d-none')
                    }
                } else {
                    items.eq(i).find('.residue-length').addClass('d-none')
                }
            }
        }
        /*
        * 根据模型号获取剩余长度*/
        function getRestlen(film_type_id) {
            var form = {
                'film_type_id': film_type_id
            };
            var url = '/admin/ajax/newPrecut';
            var params = {
                t: 'getRestlen',
            };
            form = Object.assign({}, form, params);
            request.post(url, form)
                .then(function (d) {
                    var data = d.data;
                    filmTypeRlen[data['film_type_id']] = data['restlen'];
                    updateRestlen();
                })
                .catch(function (d) {

                })
        }
        /*
        * 单部位*/
        function savePrecut(dom) {
            $(dom).attr('disabled', true);
            var form = formObjV2('.new-precut-form');
            var state = false;
            var msg;
            var a = form.part_id;
            var b = form.length;
            var c = form.product_type_id;
            var d = form.film_type_id;
            var e = form.number;
            if ((!a || !Array.isArray(a))) {
                a = [];
                if (!msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            if ((!b || !Array.isArray(b))) {
                b = [];
                if (!msg) {
                    msg = '用料长度 不能为空';
                }
            }
            if ((!e || !Array.isArray(e))) {
                b = [];
                if (!msg) {
                    msg = '生成数量 不能为空';
                }
            }
            if ((!c || !Array.isArray(c))) {
                c = [];
                if (!msg) {
                    msg = '产品类型 不能为空';
                }
            }
            if ((!d || !Array.isArray(d))) {
                d = [];
                if (!msg) {
                    msg = '膜型号 不能为空';
                }
            }
            for (var i = 0; i < a.length; i++) {
                if (!a[i] && !msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            for (var i = 0; i < b.length; i++) {
                if (!b[i] && !msg) {
                    msg = '用料长度 不能为空';
                }
            }
            for (var i = 0; i < e.length; i++) {
                if (!e[i] && !msg) {
                    msg = '生成数量 不能为空';
                }
            }
            for (var i = 0; i < c.length; i++) {
                if (!c[i] && !msg) {
                    msg = '产品类型 不能为空';
                }
            }
            for (var i = 0; i < d.length; i++) {
                if (!d[i] && !msg) {
                    msg = '膜型号 不能为空';
                }
            }
            if (msg) {
                alert(msg);
                $(dom).attr('disabled', false);
                return false;
            }
            var url = '/admin/ajax/newPrecut';
            var params = {
                t: 'store',
            };
            form = Object.assign({}, form, params);
            request.post(url, form)
                .then(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作成功！';
                    alert(msg);
                    window.location.assign('/admin/new-precut/');
                    return false;
                })
                .catch(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作失败，请重新尝试！';
                    $(dom).attr('disabled', false);
                    alert(msg);
                    return false;
                })
        }
        function savePrecutV1(dom) {
            $(dom).attr('disabled', true);
            var form = formObjV2('.new-precut-form');
            var state = false;
            var msg;
            console.log(form);
            if (!form.number || form.number < 1) {
                msg = '生成数量 不能为空';
            }
            var a = form.part_id;
            var b = form.length;
            var c = form.product_type_id;
            var d = form.film_type_id;
            if ((!a || !Array.isArray(a))) {
                a = [];
                if (!msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            if ((!b || !Array.isArray(b))) {
                b = [];
                if (!msg) {
                    msg = '用料长度 不能为空';
                }
            }
            if ((!c || !Array.isArray(c))) {
                c = [];
                if (!msg) {
                    msg = '产品类型 不能为空';
                }
            }
            if ((!d || !Array.isArray(d))) {
                d = [];
                if (!msg) {
                    msg = '膜型号 不能为空';
                }
            }
            for (var i = 0; i < a.length; i++) {
                if (!a[i] && !msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            for (var i = 0; i < b.length; i++) {
                if (!b[i] && !msg) {
                    msg = '用料长度 不能为空';
                }
            }
            for (var i = 0; i < c.length; i++) {
                if (!c[i] && !msg) {
                    msg = '产品类型 不能为空';
                }
            }
            for (var i = 0; i < d.length; i++) {
                if (!d[i] && !msg) {
                    msg = '膜型号 不能为空';
                }
            }
            if (msg) {
                alert(msg);
                $(dom).attr('disabled', false);
                return false;
            }
            var url = '/admin/ajax/newPrecut';
            var params = {
                t: 'store',
            };
            form = Object.assign({}, form, params);
            request.post(url, form)
                .then(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作成功！';
                    $(dom).attr('disabled', false);
                    alert(msg);
                    window.location.assign('/admin/new-precut/');
                    console.log(456)
                    return false;
                })
                .catch(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作失败，请重新尝试！';
                    $(dom).attr('disabled', false);
                    alert(msg);
                    console.log(d)
                    console.log(789)
                    return false;
                })
        }

        function add() {
            var part_json = new_precut_json_data.part;
            var product_json = new_precut_json_data.product_type;

            var part = '<select name="part_id[]" class="form-control part">'+makeSelectOptionsOnly(part_json)+'</select>';
            var filmtype = '<select name="film_type_id[]" class="form-control film_type">'+makeSelectOptions([])+'</select>';
            filmtype += '<div class="residue-length mt-1 d-none text-danger">剩余 <b></b> 厘米</div>';
            var length = '<input type="number" name="length[]" class="form-control">';
            var number = '<input type="number" name="number[]" class="form-control">';
            var product = '<select name="product_type_id[]" class="form-control product_type">'+makeSelectOptionsOnly(product_json)+'</select>';
            var string = '';
            string = '<tr class="item">';
            string += '<td>' + part + '</td>';
            string += '<td>' + length + '</td>';
            string += '<td>' + number + '</td>';
            string += '<td>' + product + '</td>';
            string += '<td>' + filmtype + '</td>';

            var len = $('.new-precut-coverage table tbody tr').length;
            if (len >= 1) {
                string += '<td><button type="button" class="btn btn-danger btn-sm delete">'+ts('delete')+'</button></td>';
            }  else {
                string += '<td></td>';
            }
            $('.new-precut-coverage table tbody').append(string);
        }

        function Init() {
            add();
        }
        $(document).ready(function () {
            Init();
            $('.new-precut-coverage table tbody').on('click', 'tr.item .delete',function () {
                var len = $('.new-precut-coverage table tbody tr').length;
                console.log('sdf');
                if (len > 1) {
                    $(this).parents('.new-precut-coverage table tbody tr.item').remove();
                }  else {
                }
            });
            $('.new-precut-coverage table tbody').on('change', 'tr.item .product_type',function () {
                var item = $(this).parents('.new-precut-coverage table tbody tr.item');
                var film_type = item.find(' .film_type');
                console.log(film_type.length)
                if (film_type.length>=1) {
                    var val = $(this).val();
                    var ft = coverages_data && coverages_data.filmtype;
                    var arr = [];
                    for (var i = 0; i < ft.length; i++) {
                        var item = ft[i];
                        if (item && item.product_type_id && item.product_type_id==val) {
                            arr.push(item)
                        }
                    }
                    film_type.empty().append(makeSelectOptions(arr))
                }
            });
            $('.new-precut-coverage table tbody').on('change', 'tr.item .film_type',function () {
                var val = $(this).val();
                console.log(val)
                if (val) {
                    getRestlen(val);
                } else {
                    updateRestlen();
                }
            });
        })


    </script>
@endsection